package com.example.myui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.example.myui.R;
import com.example.myui.adapter.SwipeAdapter;

import java.util.ArrayList;

public class HunPaiActivity extends AppCompatActivity {

    private Context mContext;
    private RecyclerView recyclerview;
    private SwipeRefreshLayout swipeRefreshLayout;
    private static final String TAG = HunPaiActivity.class.getSimpleName();
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            switch (what) {
                case 1001:
                    break;
                case 1002:
                    break;
                default:

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hun_pai);
        initView();
    }

    private void initView() {
        recyclerview = findViewById(R.id.rv);
        swipeRefreshLayout = findViewById(R.id.srl);
        LinearLayoutManager layoutManager = new LinearLayoutManager(HunPaiActivity.this, LinearLayoutManager.VERTICAL, false);
        recyclerview.setLayoutManager(layoutManager);
        SwipeAdapter adapter = new SwipeAdapter(HunPaiActivity.this, initData());
        swipeRefreshLayout.setRefreshing(true);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (swipeRefreshLayout.isRefreshing()) {
                    swipeRefreshLayout.setRefreshing(false);
                }
                recyclerview.setAdapter(adapter);
            }
        }, 2000);

        swipeRefreshLayout.setColorSchemeColors(Color.RED);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.i(TAG, "onRefresh: ");
                adapter.clear();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (swipeRefreshLayout.isRefreshing()) {
                            swipeRefreshLayout.setRefreshing(false);
                        }

                        adapter.setDatas(initData());
                    }
                }, 2000);

            }
        });

        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.i(TAG, "onScrollStateChanged: " + newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.i(TAG, "onScrolled: " + " dx :" + dx
                        + " dy :" + dy);
            }
        });

        adapter.setOnItemClick(new SwipeAdapter.onItemClick() {
            @Override
            public void onItemClickListener(int position) {
                Log.i(TAG, "onItemClickListener: " + position);
            }
        });
    }

    private ArrayList<String> initData() {
        ArrayList<String> datas = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            datas.add(" itemt : " + i);
        }
        return datas;
    }
}
